package com.xymtop.api.controller;

import com.xymtop.api.config.ServerStatus;
import com.xymtop.api.entity.PTeacher;
import com.xymtop.api.entity.User;
import com.xymtop.api.service.EmailService;
import com.xymtop.api.service.UserService;
import com.xymtop.api.utils.EncrypTools;
import com.xymtop.api.utils.JsonResoult;
import com.xymtop.api.utils.MyCookie;
import com.xymtop.api.utils.Timer;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private EmailService emailService;
    @RequestMapping("/add")
    public JsonResoult<String> add(@RequestParam("username") String username,@RequestParam("password") String password,
                                   @RequestParam("phone") String phone,@RequestParam("email") String email,@RequestParam("realname") String realname) {
        User user = new User();
        user.setId(Timer.getTimeStamp().toString());
        user.setUsername(username);
        user.setPassword(EncrypTools.string2MD5(password));
        user.setEmail(email);
        user.setRealname(realname);
        user.setStatus("1");//
        user.setPhone(phone);


        if (userService.add(user) == 0) {
            return new JsonResoult<>(ServerStatus.Fail);

        } else {
            emailService.sendSimpleMail(user.getEmail(),"欢迎注册","欢迎"+user.getRealname()+"的加入");
            return new JsonResoult<>(ServerStatus.SUCCESS);

        }
    }
    @RequestMapping("/delete")
        public JsonResoult<String> delete(@RequestParam("id") String id) {
            if(userService.delete(id)==0){
                return new JsonResoult<>(ServerStatus.Fail);
            }else {
                return new JsonResoult<>(ServerStatus.SUCCESS);
            }
        }

    @RequestMapping("/changphone")
        public JsonResoult<String> changphone(@RequestParam("id") String id,@RequestParam("phone") String phone)
                {
            if(userService.changPhone(id, phone)==0){
                return new JsonResoult<>(ServerStatus.Fail);
            }else {
                return new JsonResoult<>(ServerStatus.SUCCESS);
            }
        }

    @RequestMapping("/changPassword")
        public JsonResoult<String> changPassword(@RequestParam("id") String id,@RequestParam("password") String password)
                {
        if(userService.changPassword(id, password)==0){
            return new JsonResoult<>(ServerStatus.Fail);
        }else {
            return new JsonResoult<>(ServerStatus.SUCCESS);
        }
        }


        @RequestMapping("login")
    public  JsonResoult<List<User>> login(HttpServletRequest request, HttpServletResponse response, @RequestParam("username") String username, @RequestParam("password") String password
        , @RequestParam("code") String code){
        if(code==null||!code.equals(MyCookie.getCookie(request,"captcha"))){
            return new JsonResoult<>(ServerStatus.Fail,"验证码错误",null);
        }else {
            List<User> users =    userService.getList(username,EncrypTools.string2MD5(password));
            if(users.isEmpty()){
                return new JsonResoult<>(ServerStatus.Fail);
            }else {
                System.out.println(users.toString());
                MyCookie.setCookie(response,"userid",users.get(0).getId());
                return new JsonResoult<>(200,"",users);
            }
        }
    }




    //新增
    //修改
    //删除
    //查询

}
